const db = require("../db/index");
const dayjs = require('dayjs')

// 查询、
exports.dateslists = (req, res) => {
    var sql =
        "select * from dates";
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};


// 新增
exports.dateslistsinsert = (req, res) => {
    console.log(req.body)
    const { date , week , doctorlists } = req.body
    var sql =
        `insert into dates(date,week,doctorlists) value('${date}','${week}','${JSON.stringify(doctorlists)}')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};


// 查询时间段内排班数量
exports.schedutimes = (req, res) => {
    var sql =
        `select * from schedutime where doctor_id=${req.query.doctorID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

// 根据日期查询排班情况
exports.scheduDate = (req, res) => {
    var sql =
        `select * from schedutime where dates_id=${req.query.datesId} and doctor_id=${req.query.doctorId}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 医生排班查询医生排班情况
exports.doctorschedut = (req, res) => {
    var sql =
        `select * from doctor where depart_id=${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        const arr = []
        data.forEach((item,index) => {
            var sql =
                `select * from schedutime where doctor_id=${item.doctor_id} ORDER BY TimeCount_id ASC`;
            db.query(sql, [], (err, datas) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                arr.push({
                    ...item,
                    schedus:datas
                })
                if(index==data.length-1){
                    // console.log(arr,898989898989)
                    res.send(arr)
                }
                
            });
        });
        // console.log(arr,55555555)
    });
    
};


const getNext7Days = () => {
        const today = dayjs();
        const daysArray = [];

        for (let i = 0; i < 7; i++) {
            const currentDate = today.add(i, 'day');
            const formattedDate = currentDate.format('YYYY-MM-DD');
            const isToday = i === 0;
            const id = i+1
            daysArray.push({ id, date: formattedDate, isToday });
        }

        return daysArray;
};
// 新增排班

exports.insetsechedus = (req, res) => {
    const { data ,date } = req.body
    let dateid = 0
    getNext7Days().forEach(item=>{
        if(item.date==date){
            dateid = item.id
        }
    })
    if(dateid==0){
        res.send({
            message:"该日期不在排班时间内"
        })
    }else{
        data.forEach((item,index)=>{
            var sql =
                `select * from schedutime where doctor_id=${item.id} and dates_id=${dateid}`;
            db.query(sql, [], (err, datas) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                if(datas.length==0){
                    var sql =
                        `insert into schedutime(doctor_id,dates_id,timeOne,timeTwo,timeThree,timeFour,timeFive,timeSix,timeSeven,timeEight) value(${item.id},${dateid},0,0,0,${item.morningBol?item.morningShift:0},0,0,0,${item.afternoonBol?item.afternoonShift:0})`;
                    db.query(sql, [], (err, data1) => {
                        console.log(sql,2222)
                        if (err) {
                            console.log("出错了", err);
                            return res.send("错误" + err.message);
                        }
                        if(index==data.length-1){
                            res.send({
                                data1,
                                state:200
                            })
                        }
                    });
                }else{
                    var sql =
                        `update schedutime set timeEight = ${item.afternoonBol?'timeEight+'+item.afternoonShift:'timeEight'} , timeFour = ${item.morningBol?'timeFour+'+item.morningShift:'timeFour'} where doctor_id=${item.id} and dates_id= ${dateid}`;
                    db.query(sql, [], (err, data2) => {
                        console.log(sql,11111)
                        if (err) {
                            console.log("出错了", err);
                            return res.send("错误" + err.message);
                        }
                        if(index==data.length-1){
                            res.send({
                                data2,
                                state:200
                            })
                        }
                    });
                }
            });
        })
    }
    
};